package com.hzjiteng.newsbymvp.images.widget;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hzjiteng.newsbymvp.R;
import com.hzjiteng.newsbymvp.bean.ImageBean;
import com.hzjiteng.newsbymvp.images.ImageAdapter;
import com.hzjiteng.newsbymvp.images.presenter.ImagePresenter;
import com.hzjiteng.newsbymvp.images.presenter.ImagePresenterImpl;
import com.hzjiteng.newsbymvp.images.view.ImageView;

import java.util.ArrayList;
import java.util.List;


public class ImageFragment extends Fragment implements ImageView, SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private ImageAdapter adapter;
    private List<ImageBean> list;
    private ImagePresenter presenter;

    public ImageFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new ImagePresenterImpl(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blank, container, false);
        初始化数据(view);
        onRefresh();
        return view;
    }

    private void 初始化数据(View view) {
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.fragment_image_swipeRefreshLayout);
        swipeRefreshLayout.setColorSchemeColors(R.color.primary,
                R.color.primary_dark, R.color.primary_light, R.color.accent);
        swipeRefreshLayout.setOnRefreshListener(this);

        recyclerView = (RecyclerView) view.findViewById(R.id.fragment_image_recyclerView);
        recyclerView.setHasFixedSize(true);
        linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        adapter = new ImageAdapter(list,getActivity());
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(scrollListener);
    }

    private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
        private int lastVisibaleItem;
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if(newState==RecyclerView.SCROLL_STATE_IDLE&&lastVisibaleItem+1==adapter.getItemCount()){
                Snackbar.make(getActivity().findViewById(R.id.drawer_layout),R.string.image_loadmore_hint,Snackbar.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            lastVisibaleItem = linearLayoutManager.findLastVisibleItemPosition();
        }
    };

    @Override
    public void addImges(List<ImageBean> list) {
        if(this.list==null)
            this.list = new ArrayList<>();
        this.list.addAll(list);
        adapter.setList(this.list);
    }

    @Override
    public void showProgerss() {
        swipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideProgress() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showLoadFailMsg() {
        View view = getActivity() == null ? recyclerView.getRootView() : getActivity().findViewById(R.id.drawer_layout);
        Snackbar.make(view, getString(R.string.load_fail), Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void onRefresh() {
        if(list!=null){
            list.clear();
        }
        presenter.loadImageList();
    }
}
